package net.sashag.nadlandroid;

import net.sashag.nadlandroid.GestureDetectingOverlay.OnTapListener;
import android.content.Intent;
import android.location.Location;
import android.util.Log;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.apps.analytics.easytracking.TrackedMapActivity;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.Extra;
import com.googlecode.androidannotations.annotations.NoTitle;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.ViewById;
import com.googlecode.androidannotations.annotations.sharedpreferences.Pref;

@EActivity(R.layout.picklocation)
@NoTitle
public class PickLocationActivity extends TrackedMapActivity implements OnTapListener {

	@Bean protected LocationHelper locationHelper;
	@Pref protected NadlandroidPreferences_ preferences; 
	
	private MapView mapPicker;
	@ViewById protected LinearLayout llPlaceholder;
	@ViewById protected TextView txtLocation;
	@ViewById protected Button btnOK;
	
	@Extra("Address") protected String providedAddress;
	
	private Location lastGoodLocation;
	private String lastGoodAddress;
	private TrivialOverlay lastPointClickedOverlay;
	
	@AfterViews
	protected void setupPicker() {
		mapPicker = MapViewHelper.createMapView(this, llPlaceholder);
		mapPicker.setClickable(true);
		lastPointClickedOverlay = new TrivialOverlay(
				getResources().getDrawable(R.drawable.apartmentoverlay));
		mapPicker.getOverlays().add(lastPointClickedOverlay);
		mapPicker.getOverlays().add(new GestureDetectingOverlay(this, null)); //Has to be on top
		initialCenterMap();
	}
	
	@Background
	protected void initialCenterMap() {
		if (providedAddress == null) return;
		Location location = locationHelper.geocodeAddressAndReturnFirstPoint(providedAddress);
		if (location == null) return;
		GeoPoint point = new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));
		mapPicker.getController().animateTo(point);
		mapPicker.getController().setZoom(16);
		lastGoodLocation = location;
		geocodingDone(providedAddress);
	}
	
	@Click(R.id.btnOK)
	protected void onOKClicked() {
		Intent result = new Intent();
		result.putExtra("Address", lastGoodAddress);
		result.putExtra("Location", lastGoodLocation);
		setResult(RESULT_OK, result);
		finish();
	}
	
	@Override
	public void onBackPressed() {
		setResult(RESULT_CANCELED);
		finish();
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	@Background
	protected void doGeocoding(GeoPoint point) {
		Log.v("PickLocation", "Use Android geocoding: " + preferences.useAndroidGeocoding().get());
		String address = locationHelper.geocodePointAndReturnFirstAddress(point, preferences.useAndroidGeocoding().get());
		geocodingDone(address);
	}
	
	@UiThread
	protected void geocodingDone(String address) {
		lastGoodAddress = address;
		if (address != null) {
			txtLocation.setText(address);
			btnOK.setEnabled(true);
		} else {
			txtLocation.setText("Cannot retrieve the address of this locaton.");
			btnOK.setEnabled(false);
		}
	}

	@Override
	public void onTap(float x, float y) {
		GeoPoint location = mapPicker.getProjection().fromPixels((int)x, (int)y);
		lastPointClickedOverlay.setPoint(location);
		mapPicker.invalidate();
		lastGoodLocation = new Location("picked");
		lastGoodLocation.setLatitude(location.getLatitudeE6()/1E6f);
		lastGoodLocation.setLongitude(location.getLongitudeE6()/1E6f);
		txtLocation.setText("...Performing geocoding with the Google Web Service...");
		doGeocoding(location);
	}
}
